package upload;


import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URLEncoder;

import ui.FileUploadUI;
import util.FileAccess;
import util.Utility;


public class ClientFileHandle extends Thread
{
	public static final String ERROR = "error";
	public static final String FINISH = "finish";
	public static final String STOP = "stop";
	
	protected long startPos; // File Start Position
	protected long endPos;
	protected FileAccess fileAccess = null; // File Access interface
	private int id;
	private boolean stop = false;
	private String name;
	private PropertyChangeSupport status = new PropertyChangeSupport(this); 
	
	public ClientFileHandle(String path,String name,long start,long end,int id) throws IOException{
		this.startPos = start;
		this.endPos = end;
		this.id = id;
		this.name = name;
		fileAccess = new FileAccess(path, startPos);
	}
	
	public void run(){
		int bufferSize = 10240;
		long timeStamp = 0;
		int len;
		try {
			int i=0,ch,l;
			String str = "";
			String encodeName = Utility.URLEncode(name);
			byte[] b = new byte[bufferSize];
			while ((l = fileAccess.read(b,0,bufferSize)) != -1 && startPos < endPos && !stop ) {
				StringBuilder urlStr = new StringBuilder(FileUploadUI.path + "/Server?type=data&name="+encodeName+"&id="+id+"&length="+l+"&timestamp="+ timeStamp);
				Utility.send(urlStr.toString(),b,l);
				startPos += l;
				timeStamp++;
			}
			
//			while ((ch = fileAccess.read()) != -1 && startPos < endPos && !stop ) {
//				String hexValue = Integer.toHexString(ch);
//				if (hexValue.length() == 1){
//					hexValue = "0" + hexValue;
//				}
//				str += hexValue;
//				i++;
//				if(i == bufferSize){
//					StringBuilder urlStr = new StringBuilder(FileUploadUI.path + "/Server?type=data&name="+encodeName+"&id="+id+"&length="+i+"&timestamp="+ timeStamp +"&data=");
//					urlStr.append(str);
//					Utility.send(urlStr.toString());
//					
//					startPos += bufferSize;
//					timeStamp++;
//					
//					i=0;
//					str="";
//				}
//			}
//			if(str.length()!=0){
//				StringBuilder urlStr = new StringBuilder(FileUploadUI.path + "/Server?type=data&name="+encodeName+"&id="+id+"&length="+i+"&timestamp="+ timeStamp +"&data=");
//				urlStr.append(str);
//				Utility.send(urlStr.toString());
//				
//				startPos += bufferSize;
//				timeStamp++;
//				
//				i=0;
//				str="";
//			}
			
			
			if(startPos >= endPos)
				status.firePropertyChange(FINISH, null, this);
			else
				status.firePropertyChange(STOP, null, this);
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public int getID(){
		return id;
	}
	
	public void stopUpload(){
		stop = true;
	}
	
	public boolean isStop(){
		return stop;
	}
	
	public long getStartPos(){
		return startPos;
	}

	public long getEndPos(){
		return endPos;
	}
	
	public void addPropertyChangeListener(PropertyChangeListener l) {
		status.addPropertyChangeListener(l);
	}

	public void removePropertyChangeListener(PropertyChangeListener l) {
		status.removePropertyChangeListener(l);
	}
}
